<template>

	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>


			</el-form>
		</el-col>

		<!--显示列表-->
    <el-table :data="Dictionarydetail" highlight-current-row v-loading="listLoading"   @selection-change="selsChange"
              border :visible="drawer" @row-dblclick="rowDrawerClick"
              style="width: 100%;">

      <!-- 批量删除：type="selection - 选项 -->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column @click="" prop="name" label="字典明细名称" >
      </el-table-column>
      <el-table-column prop="dictionarytype.name" label="字典明细类型">
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small"
                     @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>

      <!-- 实现分页
      layout：布局
      @current-change：当前页发生变化调用的方法
      :current-page： 当前页
      :page-size：当前页显示条数
      total：数据总条数
      style ：风格
      -->
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                  :current-page="currentPage"   :page-size="pageSize" :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

    <!--添加和修改的对话框-->
    <!--
      :close-on-click-modal="false" - 点击对话外面的区域不关闭对话框
      :model="saveForm" - 绑定表单数据
      :rules="saveFormRules" - 校验规则
      ref="saveForm" - 对话框的名称 - 验证的时候需要用
      auto-complete="off" - 自动完成
    -->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false" name="">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="字典详细名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input></el-form-item>
        <el-form-item label="字典详细类型">
          <el-select v-model="saveForm.types_id" placeholder="请选择">
                <el-option v-for="item in dictionarytype"
                           :label="item.name"
                           :value="item.id">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>


</template>
<script>
	export default {
		data() {
			return {
        //默认不显示抽屉
        drawer: false,
        //多层抽屉
        innerDrawer: false,

        direction: 'rtl',
        //1.分页数据 - 要与模板中对应起来
        //分页查询 - 显示分页列表数据
        /* 数据展示的地方 */
        Dictionarydetail: [],
        //分页查询 - 总数量 - 计算分页栏
        totals: 0,
        //每页显示的条数
        pageSize: 5,
        //当前页 -  - 分页栏显示页数
        currentPage: 1,
        //分页列表是否显示
        listLoading: false,
        // 高级查询所需数据
        keyword:"",
        //批量删除 列表选中列
				sels:[],
        // 加载字典类型
        dictionarytype:[],

        table:false,
        //添加或修改数据
        saveFormVisible: false,//添加或修改界面是否显示
        saveLoading: false,
        title: '',
        saveFormRules: {
          types_id: [
            { required: true, message: '请输入字典明细类型', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入字典明细名称', trigger: 'blur' }
          ]
        },
        //编辑界面数据
        saveForm: {
          id: null,
          types_id:null,
          name: '',
        },

			}
		},
		methods: {
      //双击显示内层抽屉
      rowDrawerClick(row) {
        // console.debug(row)
        //每次子表打开，页码重新赋值为1
        this.drawerCurrentPage = 1
        //每次子表打开，每页数据条数重新赋值为5条
        this.drawerPageSize = 5
        //品牌id赋值
        this.formDrawerInline.parentId = row.id
        //展示内层抽屉列表
        this.loadRowDrawerDataByQuery();
        //弹出内层抽屉
        this.innerDrawer = true
      },

      // 抽屉
      handleClose(done) {
        this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});

      },
		  // 关键字查询
      keywordQuery(){
        this.currentPage=1;
        this.getdictionarydetail();
      },

      //获取部门列表
      getdictionarydetail() {
        let para = {
          //当前页
          currentPage: this.currentPage,
          //每页显示的条数
          pageSize: this.pageSize,
          // 传递参数到后端
          keyword: this.keyword
        };


        //显示加载框/忙等框
        this.listLoading = true;
        // 分页 查询

        this.$http.post("/dictionarydetail",para).then(res=>{
          //给totals赋值 - 计算分页栏
          this.totals = res.data.totals;
          //赋值分页数据 - 显示当前页的分页数据
          this.Dictionarydetail = res.data.data;
          //隐藏加载框/忙等框
          this.listLoading = false;
        }).catch(res=>{
          alert("系统错误，请稍后重试!");
        });
      },
      //选项改变触发此方法(批量删除)：获取选中的对象
      selsChange: function (sels) {
        this.sels = sels;
      },

      // 分页换页
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getdictionarydetail();
			},

			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dictionarydetail/"+row.id).then(res=> {
            this.listLoading = false;
            /*
             删除页最后一条 跳转 新一页
            success 成功运行*/
            if(res.data.success){

              //计算总页数
              let totalPage = this.totals%this.pageSize==0?this.totals/this.pageSize
                  :Math.ceil(this.totals/this.pageSize);
              // 当前页>1 并且当前页是最后一页 并且 是最后一条数据
              if(this.currentPage > 1 && this.currentPage==totalPage && (this.totals-1)%this.pageSize==0){
                // 当前页= 当前页-1
                // 跳转到新一页
                this.currentPage = this.currentPage - 1;
              }
              this.$message.success("操作成功");
              this.getdictionarydetail();
            }else{
              this.$message.error("删除失败【500】");
            }
          }).catch(res => {
            this.$message.error("系统错误【400,404】");
          })
        }).catch(() => {
          //确认框点击取消之后走这里
        });
      },
      //批量删除
      batchRemove: function () {

        var ids = this.sels.map(item => item.id);
        console.log(ids);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();

          this.$http.patch("/dictionarydetail",ids).then(res=>{
            if(res.data.success){
              console.log(ids);
              this.$message.success("操作成功");
              //计算总页数
              let totalPage = this.totals%this.pageSize==0?this.totals/this.pageSize
                  :Math.ceil(this.totals/this.pageSize);
              // 当前页>1 并且当前页是最后一页 并且 是最后一条数据
              if(this.currentPage > 1 && this.currentPage==totalPage && (this.totals-1)%this.pageSize==0){
                // 当前页= 当前页-1
                // 跳转到新一页
                this.currentPage = this.currentPage - 1;
              }
              this.getdictionarydetail();
            }else{
              this.$message.error("操作失败【500】");
            }
            this.listLoading = false;
          }).catch((res)=>{
            this.$message.error("操作失败【400,404】")
          })
          // 取消点击 确定框
        }).catch(() => {
        });
      },

      //点击修改按钮弹出模态框
      handleEdit: function (index, row) {

        this.title = "修改";
        this.saveForm = Object.assign({}, row);
        this.getDictionarytypes();
        this.saveFormVisible = true;


      },

//点击添加按钮弹出模态框
      handleAdd: function () {
        this.title = "添加";
        this.saveForm = {
          id: null,
          sn: '',
          name: '',
          state: 1,
          manager_id: null,
          manager: null,
          parent_id: null,
          parent: null,


        };
        this.getDictionarytypes();
        this.saveFormVisible = true;

      },
      //点击对话框提交数据
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              let paras = Object.assign({}, this.saveForm);

              //对父部门做处理
              var parentTmp = paras.parent;// [2,7,17]  17是父部门
              if(parentTmp){
                paras.parent = null;
                paras.parent_id = parentTmp[parentTmp.length-1];
              }

              this.$http.put("/dictionarydetail",paras).then(res => {
                this.saveFormVisible = false;
                this.saveLoading = false;
                if(res.data.success){
                  this.getdictionarydetail();
                }else{
                  this.$message.error("操作失败【500】");
                }
              }).catch(res => {
                this.$message.error("系统错误【400,404】");
              })
            });
          }
        });
      },

      // 获取字典名称
      getDictionarytypes(){
        this.$http.get("/dictionarytype").then(res =>{
          this.dictionarytype =res.data;
        })
      },
    },


    // 页面加载就显示
		mounted() {
			this.getdictionarydetail();
		},
	}


</script>





